package lumis.service.wsrp.consumer;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.xml.namespace.QName;
import javax.xml.ws.Holder;

import lumis.portal.PortalException;

import com.sun.portal.wsrp.common.stubs.v2.Extension;
import com.sun.portal.wsrp.common.stubs.v2.FailedPortlets;
import com.sun.portal.wsrp.common.stubs.v2.Lifetime;
import com.sun.portal.wsrp.common.stubs.v2.ModelDescription;
import com.sun.portal.wsrp.common.stubs.v2.PortletContext;
import com.sun.portal.wsrp.common.stubs.v2.PortletDescription;
import com.sun.portal.wsrp.common.stubs.v2.Property;
import com.sun.portal.wsrp.common.stubs.v2.PropertyDescription;
import com.sun.portal.wsrp.common.stubs.v2.PropertyList;
import com.sun.portal.wsrp.common.stubs.v2.RegistrationContext;
import com.sun.portal.wsrp.common.stubs.v2.ResetProperty;
import com.sun.portal.wsrp.common.stubs.v2.ResourceList;
import com.sun.portal.wsrp.common.stubs.v2.UserContext;

/**
 * Helper for remote portlet version 2.
 *
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class RemotePortletHelperV2 extends RemotePortletHelper 
{
	RemotePortletHelperV2(String portletHandle, RegistrationContext registrationContext)
	{
		super(portletHandle, registrationContext);
	}
		
	PortletContext getPortletContextV2()
	{
		PortletContext portletContext = new PortletContext();
		portletContext.setPortletHandle(this.portletHandle);
		
		return portletContext;
	}
	
	UserContext getEmptyUserContextV2()
	{
		UserContext userContext = new UserContext();
		return userContext;
	}
		
	Lifetime getLifetimeV2() throws PortalException
	{
		return getRegistrationContextV2().getScheduledDestruction();
	}
	
	Holder<Lifetime> getEmptyLifetimeHolderV2()
	{
		Holder<Lifetime> lifetimeHolder = new Holder<Lifetime>();
		return lifetimeHolder;
	}
	
	Holder<List<Property>> getEmptyPropertiesHolderV2()
	{
		Holder<List<Property>> propertiesHolder = new Holder<List<Property>>();
		propertiesHolder.value = new ArrayList<Property>();
		return propertiesHolder;
	}
	
	Holder<List<ResetProperty>> getEmptyResetPropertyListHolderV2()
	{
		Holder<List<ResetProperty>> resetProperties = new Holder<List<ResetProperty>>();
		resetProperties.value = new ArrayList<ResetProperty>();
		return resetProperties;
	}
	
	Holder<ModelDescription> getEmptyModelDescriptionHolderV2()
	{
		Holder<ModelDescription> modelDescriptionHolder = new Holder<ModelDescription>();
		return modelDescriptionHolder;
	}
	
	Holder<ResourceList> getEmptyResourceListHolderV2()
	{
		Holder<ResourceList> resourceListHolder = new Holder<ResourceList>();
		return resourceListHolder;
	}
	
	Holder<List<FailedPortlets>> getEmptyFailedPortletsListHolderV2()
	{
		Holder<List<FailedPortlets>> holder = new Holder<List<FailedPortlets>>();
		return holder;
	}
	
	Holder<List<Extension>> getEmptyExtensionListHolderV2() throws PortalException
	{
		Holder<List<Extension>> holder = new Holder<List<Extension>>();
		holder.value = new ArrayList<Extension>();
		return holder;
	}
	
	Holder<PortletDescription> getEmptyPortletDescriptionHolderV2()
	{
		Holder<PortletDescription> holder = new Holder<PortletDescription>();
		return holder;
	}

	static Properties toProperties(Holder<List<Property>> v2Properties)
	{
		Properties properties = new Properties();
		for (Property v2Property : v2Properties.value)
		{
			properties.put(v2Property.getName(), v2Property.getStringValue());
		}
		return properties;
	}
	
	static String toPropertyDescription(Holder<ModelDescription> modelDescription, String propertyName)
	{
		if (modelDescription != null)
		{
			for (PropertyDescription propertyDescription: modelDescription.value.getPropertyDescriptions())
			{
				if (propertyDescription.getName().equals(propertyName))
				{
					return propertyDescription.getDescription().getValue();
				}
			}
		}
		return null;
	}
	
	static PropertyList toPropertyListV2(Properties properties)
	{
		PropertyList propertyList = new PropertyList();		
		for (Object key : properties.keySet())
		{
			Property v2Property = new Property();			
			QName name = new QName((String)key);			
			v2Property.setName(name);
			v2Property.setStringValue(properties.getProperty((String) key));
			
			propertyList.getProperties().add(v2Property);
		}
		return propertyList;
	}	
}